package com.gopay.bis.web.controller.fund;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.common.FundResult;
import com.gopay.common.domain.fund.vo.PageData;
import com.gopay.remote.bank.BankInteractRemoteService;
import com.gopay.remote.bank.FundInteractRemoteService;

/**
 * 基金回应基类
 * @FundResBase.java
 * @author dong-wang5@hnari.com
 * @2016年6月16日 上午10:59:25  www.gopay.com.cn Inc.All rights reserved.
 */
public abstract class FundResBase {
	
	private static Log logger = LogFactory.getLog(FundResBase.class);

	@Resource(name = "bisConfig")
	protected Map<String, String> bisConfig;

	@Resource(name = "fundInteractService")
	private FundInteractRemoteService fundInteractService;

	@Resource(name = "bisInteractExternalService")
	BankInteractRemoteService bisInteractExternalService;
	
	/**
	 * 验证返回参数
	 * 
	 * @param pageData
	 * @return
	 */
	protected abstract boolean checkBase(PageData pageData);

	/**
	 * 银行通知国付宝
	 * 
	 * @param pageData
	 * @return
	 */
	protected String execute(PageData pageData) {
		// 打印请求参数
		this.displayRequestParameters(pageData, logger);
		//校验参数
		boolean isCheck = checkBase(pageData);
		FundResult fundResult = new FundResult();
		fundResult.setIsSuccess("F");
		try {
			if(isCheck){
				String notifyType = pageData.getString("NOTIFY_TYPE");
				if(StringUtils.isNotBlank(notifyType) && "SEND".equals(notifyType)){
					logger.error("国付宝通知监管行通知开始！！！！");
					//国付宝通知银行
					fundResult = fundInteractService.send(pageData);
				}else{
					logger.error("监管行通知国付宝通知开始！！！！");
					//银行通知国付宝
					//验签，并保存数据
					fundResult = fundInteractService.receive(pageData);
				}
			}else{
				fundResult.setErrorCode(ProcCodeConstants.PROC_CODE_100E7023);
				fundResult.setErrorMsg("银行返回通知参数为空");
			}
		} catch (Exception e) {
			logger.error("中信银行基金联调接口异常", e);
			fundResult.setErrorCode(ProcCodeConstants.PROC_CODE_100F1002);
			fundResult.setErrorMsg(e.getMessage());
		}
		return fundResult.toString();
	}

	/**
	 * 输出请求参数
	 * 
	 * @param pageData
	 */
	@SuppressWarnings("rawtypes")
	protected void displayRequestParameters(PageData pageData,Log logger) {
		for (Object s : pageData.keySet()) {
			if (logger != null) {
				logger.error("map key: [" + s + "]，value: [" + pageData.getString(s)+ "]");
			}
		}
	}

}
